package com.visec.hdy.task;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.visec.hdy.config.PCVHandle;
import com.visec.hdy.sync.ftp.code.DispatchAnswer;
import com.visec.hdy.sync.ftp.code.GetManager;
import com.visec.hdy.transmission.rmi.config.RMIModel;
import com.visec.hdy.transmission.rmi.config.RMIModelCV;
import com.visec.hdy.transmission.socket.SocketFactory;
import com.visec.hdy.transmission.socket.code.SocketServer;
import com.visec.hdy.transmission.socket.config.SSocketModelCV;
import com.visec.hdy.transmission.socket.config.SocketModel;
import com.visec.hdy.utils.ParameterHandle;

public class SyncServer {
	// -sip 192.168.010.165 -sport 4401 -task server
	public static void main(String[] args) {
		args = new String[] { "-sip", "192.168.10.165", "-sport", "4401" };
		Map<String, String> m = ParameterHandle.strArryToMap(args);
		start(m);
	}

	public static void start(Map<String, String> m) {
		// args = new String[] { "-sip", "192.168.010.165", "-sport", "4401" };

		PCVHandle pcv = new PCVHandle(new SSocketModelCV(), new RMIModelCV());
		pcv.conversion(m);

		SocketModel socket = pcv.getTarget(SocketModel.class);
		RMIModel rmi = pcv.getTarget(RMIModel.class);

		if (socket.getIp() == null) {
			System.out.println("oh my god,why you send a bad ip by -sip!");
			return;
		}
		if (socket.getPort() == 0) {
			System.out.println("oh my god,why you send a bad port by -sport!");
			return;
		}
		Map<String, GetManager> fgm = Collections.synchronizedMap(new HashMap<String, GetManager>());

		Thread t = new Thread(new ServerTaskReceive(fgm, rmi, socket.getBufferSize()));
		t.setName("rmi");
		t.setDaemon(true);
		t.start();
		// fgm.put("sb",
		// FtpGetManager.getInstance(
		// new SyncGetConfig(new FtpModel("localhost", 21, "admin", "123456",
		// "utf-8", "iso-8859-1"), "sb",
		// new FtpFileManager(), 2048)));

		SocketServer createServer = SocketFactory.getInstance().createServer(socket);

		System.out.println("server start!-sip:" + socket.getIp() + ",-sport:" + socket.getPort() + ",-scharset:"
				+ socket.getCharset() + ",-sbufferSize:" + socket.getBufferSize());

		createServer.answer(new DispatchAnswer(fgm), false);

	}

}
